{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb297a85",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "\n",
    "> * list一般方法/函数 的所有的使用场景及案例，方法如：\n",
    ">> 1. append()\n",
    ">> 2. pop(index)\\remove(values)\n",
    ">> 3. count()\n",
    ">> 4. index()\n",
    ">> 5. extend()\n",
    ">> 6. sort()\n",
    ">> 7. ...\n",
    "> * list高级方法 如：\n",
    ">> 1. copy() 复制 不等于 赋值， 复制可 等价于 切片[：]\n",
    ">> 2. enumerate\n",
    "> * 课本练习（课堂上带大家做了）Don't panic ==> on tap,其中知识点：\n",
    ">> 1.list的一些方法实现\n",
    ">> 2.list的切片实现\n",
    ">> 3.str => list 的方法 list（str）\n",
    ">> 4.list=> str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10553021",
   "metadata": {},
   "source": [
    "# 先做简单的练习\n",
    "\n",
    "* 1. vowels2. py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "43907966",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u'] # 这是元音（list）\n",
    "word = \"Milliways\" # 这是文本\n",
    "# 需求： word中出现的 vowels 打印出来\n",
    "# 循环遍历的第一个位置的元素请注意名字不要和已经定义过的变量重复 \n",
    "# for循环的第二个元素，决定了我们循环的内容，循环的列表，每个元素单独在一行打印\n",
    "# i这个位置可以随意命名，不会影响我们的结果\n",
    "# 一般情况下，item（内容）=>简写：i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c4301e99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "for i in word:\n",
    " #   print(i)\n",
    "    if i in vowels:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4853bc14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# append()经常用于新建列表\n",
    "found = []\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        if i not in found:\n",
    "            found.append(i)\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9c473af",
   "metadata": {},
   "source": [
    "# 数据结构练习1\n",
    "\n",
    "* 购物车程序项目：\n",
    "* 要求：\n",
    "1. 运行程序后，让用户输入支付宝余额，然后打印我们商品列表给用户。\n",
    "2. 让用户输入商品编号进行商品的购买。\n",
    "3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70658695",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "# 1. 商品信息\n",
    "goods_list = [\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",199],\n",
    "    [\"蜜雪冰城奶茶\",8]\n",
    "]\n",
    "# 2. 用户购物信息\n",
    "user_shopping_list = [......]\n",
    "# 用户输入（余额）：  # ------知识点： 打印与用户输入------\n",
    "Alipay = input(\"请输入您的支付宝余额：\")\n",
    "info = '''------goods detail-------\n",
    "编号0，商品名称： %s，商品价格： %s\n",
    "编号1，商品名称： %s，商品价格： %s\n",
    "编号2，商品名称： %s，商品价格： %s\n",
    "编号3，商品名称： %s，商品价格： %s\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "     goods_list[1][0],goods_list[1][1],\n",
    "     goods_list[2][0],goods_list[2][1],\n",
    "     goods_list[3][0],goods_list[3][1],\n",
    "    )\n",
    "print(info)\n",
    "\n",
    "# 是不是应该循环了呢？\n",
    "\n",
    "# 1. 输入正确，打印商品列表\n",
    "## 用户输入商品编号\n",
    "## 1. 输入编号存在，判断余额是否够\n",
    "### 1. 余额够，用户余额 - 商品价格\n",
    "### 2. 余额不够，“您的余额不足”\n",
    "## 2. 输入的编号不存在，“请输入正确的余额”\n",
    "# 2. 输入错误，“请输入正确的余额”\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9986d404",
   "metadata": {},
   "source": [
    "# 作业补充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f92d63c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------goods detail-------\n",
      "编号0，商品名称： python课本，商品价格： 60\n",
      "编号1，商品名称： 华为手机，商品价格： 8000\n",
      "编号2，商品名称： 小米手环，商品价格： 199\n",
      "编号3，商品名称： 蜜雪冰城奶茶，商品价格： 8\n",
      "\n",
      "------goods detail-------\n",
      "请输入您的支付宝余额：899\n",
      "输入错误，您还有2次机会！\n",
      "请输入您的支付宝余额：6800\n",
      "您输入的支付宝余额正确！\n",
      "请输入购买商品的编号：4\n",
      "您输入的编号不存在，请重新输入！\n",
      "重新输入购买商品的编号：2\n",
      "您的商品购买成功，已添加到购物车，您的余额还有6601\n",
      "购买清单\n",
      "编号2，商品名称小米手环：199元\n",
      "over!\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    " 数据结构练习1\n",
    "\n",
    "* 购物车程序项目：\n",
    "* 要求：\n",
    "1. 运行程序后，让用户输入支付宝余额，然后打印我们商品列表给用户。\n",
    "2. 让用户输入商品编号进行商品的购买。\n",
    "3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\"\"\"\n",
    "\n",
    "# 准备工作\n",
    "# 1. 商品信息\n",
    "goods_list = [\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",199],\n",
    "    [\"蜜雪冰城奶茶\",8]\n",
    "]\n",
    "\n",
    "\n",
    "info = '''------goods detail-------\n",
    "编号0，商品名称： %s，商品价格： %s\n",
    "编号1，商品名称： %s，商品价格： %s\n",
    "编号2，商品名称： %s，商品价格： %s\n",
    "编号3，商品名称： %s，商品价格： %s\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "     goods_list[1][0],goods_list[1][1],\n",
    "     goods_list[2][0],goods_list[2][1],\n",
    "     goods_list[3][0],goods_list[3][1],\n",
    "    )\n",
    "print(info)\n",
    "# 是不是应该循环了呢？\n",
    "while True:\n",
    "    print(\"------goods detail-------\")\n",
    "    break\n",
    "    # for循环优势，节省重复代码的书写\n",
    "    for i in range(4):  # i是0，1，2，3\n",
    "        print(\"编号%s, 商品名称%s：%s元\" %(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "# 2. 用户购物信息\n",
    "user_shopping_list = []\n",
    "# 用户输入（余额）：  # ------知识点： 打印与用户输入------\n",
    "\n",
    "count = 3\n",
    "while True:\n",
    "    money_balance = 6800\n",
    "    Alipay = int(input(\"请输入您的支付宝余额：\"))\n",
    "    count -=1\n",
    "\n",
    "    if Alipay == money_balance:\n",
    "        print(\"您输入的支付宝余额正确！\")\n",
    "        choice = int(input(\"请输入购买商品的编号：\"))\n",
    "        ## 1. 输入编号存在，判断余额是否够\n",
    "        if choice < len(goods_list):\n",
    "            ### 1. 余额够，用户余额 - 商品价格\n",
    "            if goods_list[choice][1] <= Alipay:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                print(\"购买清单\\n编号%s，商品名称%s：%s元\"%(choice,goods_list[choice][0],goods_list[choice][1]))\n",
    "                break\n",
    "            ### 2. 余额不够，“您的余额不足”\n",
    "            else:\n",
    "                print(\"不好意思，您的余额不足！无法购买\")\n",
    "        ## 2. 输入的编号不存在，“请输入正确的余额”\n",
    "        # 2. 输入错误，“请输入正确的余额”\n",
    "        else:\n",
    "            print(\"您输入的编号不存在，请重新输入！\")\n",
    "            choice_2 = int(input(\"重新输入购买商品的编号：\"))\n",
    "            if choice_2 < len(goods_list):\n",
    "                ### 1. 余额够，用户余额 - 商品价格\n",
    "                if goods_list[choice_2][1] <= Alipay:\n",
    "                    Alipay = Alipay - goods_list[choice_2][1]\n",
    "                    print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                    print(\"购买清单\\n编号%s，商品名称%s：%s元\" % (choice_2, goods_list[choice_2][0], goods_list[choice_2][1]))\n",
    "                    break\n",
    "\n",
    "                ### 2. 余额不够，“您的余额不足”\n",
    "                else:\n",
    "                    print(\"不好意思，您的余额不足！无法购买\")\n",
    "                break\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    else:\n",
    "        if count == 0:\n",
    "            print(\"您的账号无法正常使用！\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"输入错误，您还有%d次机会！\" % count)\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de423c9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
